import {
  getOrdersByUserAndOrg_service,
  getStudentInfo_simple_service,
  getStudentsByOrgId,
  StudentType
} from "../../graphql/student";
import {useLazyQuery, useQuery} from "@apollo/client";
import { PageType } from "../../types";
import {OrderType} from "../../types/order";

export const useGetStudents = () => {
  const [fun, { loading }] = useLazyQuery(getStudentsByOrgId, {
    fetchPolicy: "network-only"
  });

  const getStudents = async (
    pageSize: number,
    pageNum: number,
    orgId: string,
    name?: string
  ) => {
    const res = await fun({
      variables: {
        page: {
          pageSize,
          pageNum
        },
        orgId,
        name
      }
    });
    return {
      data: res?.data?.getStudentsByOrders.data as StudentType[],
      page: res?.data?.getStudentsByOrders.page as PageType
    };
  };

  return {
    getStudents,
    loading
  };
};

export const useGetStudentInfo_simple = () => {
  const [fun, { loading }] = useLazyQuery(getStudentInfo_simple_service);
  const getStudentInfo = async (id: string) => {
    const res = await fun({
      variables: {
        id
      }
    });
    return res.data.getStudentInfoPc.data as StudentType;
  };
  return { getStudentInfo, loading };
};

export const useGetOrdersByUserAndOrg = (transmitUserId: string) => {
  const { data } = useQuery(getOrdersByUserAndOrg_service, {
    variables: {
      transmitUserId,
    },
    fetchPolicy: "network-only",
  });
  return { data: data?.getOrdersByUserId.data as OrderType[] } as {
    data: OrderType[];
  };
};
